<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
      :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">

          <!--          <el-form-item label="团课" prop="classId">
            <el-select v-model="queryParams.classId" placeholder="请选择" @keyup.enter="handleQuery" clearable filterable>
              <el-option v-for="dict in leagueClassList" :key="dict.id" :label="dict.leagueClassName"
                :value="dict.id"></el-option>
            </el-select>
          </el-form-item> -->
          <el-form-item label="教练" prop="coachId">
            <el-select v-model="queryParams.coachId" placeholder="请选择" filterable>
              <el-option v-for="dict in coachList" :key="dict.id" :label="dict.coachName" :value="dict.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="开始时间">
            <el-date-picker clearable v-model="queryParams.startDate" type="date" value-format="YYYY-MM-DD 00:00:00"
              placeholder="请选择开始时间">
            </el-date-picker>
          </el-form-item>

          <el-form-item label="结束时间">
            <el-date-picker clearable v-model="queryParams.endDate" type="date" value-format="YYYY-MM-DD 23:59:59"
              placeholder="请选择结束时间">
            </el-date-picker>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd"
              v-hasPermi="['primary:privateTeachingSchedule:add']">新增</el-button>
          </el-col>
          <!--          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()"
              v-hasPermi="['primary:privateTeachingSchedule:edit']">修改</el-button>
          </el-col> -->
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()"
              v-hasPermi="['primary:privateTeachingSchedule:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport"
              v-hasPermi="['primary:privateTeachingSchedule:export']">导出</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handledate()">设置私教预约时间</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="privateTeachingScheduleList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <!-- <el-table-column label="id" align="center" prop="id" v-if="true" /> -->
        <!-- <el-table-column label="健身房id" align="center" prop="gymId" /> -->
        <el-table-column label="会员名称" align="center" prop="memberName" />
        <!-- <el-table-column label="会员卡id" align="center" prop="cardId" /> -->
        <el-table-column label="预约日期" align="center" prop="reservationDate" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.reservationDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="课程名称" align="center" prop="cardName" />
        <el-table-column label="教练名称" align="center" prop="coachName" />
        <el-table-column label="课程开始时间" align="center" prop="teachStartTime" width="180">
          <!--          <template #default="scope">
            <span>{{ parseTime(scope.row.teachStartTime, '{y}-{m}-{d}') }}</span>
          </template> -->
        </el-table-column>
        <el-table-column label="课程结束时间" align="center" prop="teachEndTime" width="180" />
        <!--       <el-table-column label="授课方式" align="center" prop="teachType">
          <template v-slot="{ row }">
            {{ row.teachType == 0 ? '一对一' : '一对多' }}
          </template>
        </el-table-column> -->
        <el-table-column label="是否可以预约" align="center" prop="isReservation">
          <template v-slot="{ row }">
            {{ row.isReservation == 0 ? '不可以' : '可以' }}
          </template>
        </el-table-column>


        <el-table-column label="预约状态" align="center">
          <template #default="scope">
            <dict-tag :options="sys_yuyuestatus_type" :value="scope.row.reservationSituation" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <!--            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['primary:privateTeachingSchedule:edit']"></el-button>
            </el-tooltip> -->
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                v-hasPermi="['primary:privateTeachingSchedule:remove']"></el-button>
            </el-tooltip>
            <el-tooltip content="查看详情" placement="top">
              <el-button link type="primary" icon="User" @click="handleinfo(scope.row)"
                v-hasPermi="['primary:privateTeachingSchedule:edit']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改私教预定日期对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="580px" append-to-body>
      <el-form ref="privateTeachingScheduleFormRef" :model="form" :rules="rules" label-width="130px">

        <el-form-item label="选择预约人" prop="memberId" v-if="isshow==1">
          <!-- <span>{{form.memberId}}会员id</span> -->
          <el-select ref="selectRef" v-model="form.memberId" placeholder="请选择" filterable clearable v-if="isSearch"
            @change="changemember">
            <el-option v-for="dict in memberList" :key="dict.id" :label="dict.memberName" :value="dict.id"></el-option>
          </el-select>
          <el-input v-model="form.memberName" placeholder="请输入会员名进行搜索" type="text" style="width: 215px;" v-else
            @input="trimInput('memberName')" />
          <el-button type="primary" icon="Search" @click="getListvip(form.memberName)">搜索</el-button>
        </el-form-item>

        <!--  -->
        <el-form-item label="选择会员卡" prop="memberCardId" v-if="isshowMemberCard==4 && dialog.isfalse==false">
          <el-select v-model="form.memberCardId" placeholder="请选择" clearable :disabled='dialog.isfalse'
            @change="handleSelectChange">
            <el-option v-for="dict in cardList" :key="dict.id" :label="dict.cardName" :value="dict.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="教练名称" :inline="true" v-if="isshowMemberCard==4 && dialog.isfalse==false">
          <!-- <span>{{form.coachId}}</span> -->
          <el-input v-model="form.coachName" placeholder="请输入教练名称" type="text" style="width: 215px;" :disabled='true' />
          <!--          <el-select v-model="form.coachId" placeholder="请选择" clearable filterable :disabled='dialog.isfalse'
            @change="changecoach">
            <el-option v-for="dict in coachList" :key="dict.id" :label="dict.coachName" :value="dict.id"></el-option>
          </el-select> -->
          <!-- <el-button type="primary" icon="Search" @click="handlcoach(form.coachId)" v-if=" isshow==1">搜索</el-button> -->
        </el-form-item>

        <!--        <el-form-item label="选择教练会员卡" prop="cardId" v-if="isxiana==10">
          <el-select v-model="form.cardId" placeholder="请选择" clearable filterable :disabled='dialog.isfalse'>
            <el-option v-for="dict in coachcardList" :key="dict.cardId" :label="dict.cardName"
              :value="dict.cardId"></el-option>
          </el-select>
        </el-form-item> -->


        <!--        <el-form-item label="预约日期" prop="reservationDate" v-if="isshow!=10">
          <el-date-picker clearable v-model="form.reservationDate" type="datetime" value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择预约日期" :disabled='dialog.isfalse'>
          </el-date-picker>
        </el-form-item> -->

        <el-form-item label="预约日期" prop="reservationDate" v-if="isshow!=10">
          <el-date-picker clearable v-model="form.reservationDate" type="date" value-format="YYYY-MM-DD 00:00:00"
            placeholder="请选择预约日期" :disabled='dialog.isfalse'>
          </el-date-picker>
        </el-form-item>

        <el-form-item label="课程时间" prop="teachStartTime" v-if="isshow!=10">
          <el-time-select v-model="form.teachStartTime" style="width: 215px" class="mr-4" placeholder="课程时间"
            start="01:00" step="00:15" end="23:59" :disabled='dialog.isfalse' />
        </el-form-item>

        <el-form-item label="私教开始时间" prop="StartTime" v-if="isshow==10">
          <el-time-select v-model="form.StartTime" style="width: 215px" class="mr-4" placeholder="课程时间" start="01:00"
            step="00:15" end="23:59" :disabled='dialog.isfalse' />
        </el-form-item>

        <el-form-item label="私教结束时间" prop="EndTime" v-if="isshow==10">
          <el-time-select v-model="form.EndTime" style="width: 215px" class="mr-4" placeholder="课程时间" start="01:00"
            step="00:15" end="23:59" :disabled='dialog.isfalse' />
        </el-form-item>

        <!--        <el-form-item label="课程时间" prop="teachDate">
          <el-date-picker clearable v-model="form.teachDate" type="datetime" value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择课程时间">
          </el-date-picker>
        </el-form-item> -->
        <!--        <el-form-item label="授课方式" prop="teachType">
          <el-radio-group v-model="form.teachType" :disabled='dialog.isfalse'>
            <el-radio v-for="istype in dataa.teachType" :key="istype.value" :label="istype.value">
              {{ istype.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item> -->

        <!--       <el-form-item label="是否可以预约" prop="isReservation">
          <el-radio-group v-model="form.isReservation" :disabled='dialog.isfalse'>
            <el-radio v-for="isyuyue in dataa.yuyue" :key="isyuyue.value" :label="isyuyue.value">
              {{ isyuyue.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item> -->
        <!--        <el-form-item label="预约状态" prop="reservationSituation">
          <el-radio-group v-model="form.reservationSituation" :disabled='dialog.isfalse'>
            <el-radio v-for="zhuangtai in dataa.state" :key="zhuangtai.value" :label="zhuangtai.value">
              {{ zhuangtai.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item> -->
        <!--        <el-form-item label="选择预约人" :inline="true" v-if="isshow==1">
          <el-select v-model="form.memberId" placeholder="请选择" clearable filterable>
            <el-option v-for="dict in memberList" :key="dict.id" :label="dict.memberName" :value="dict.id"></el-option>
          </el-select>
        </el-form-item> -->


      </el-form>
      <el-card shadow="never" v-if="isshow==3">
        <el-table v-loading="loading" :data="form.memberList">
          <el-table-column label="会员姓名" align="center" prop="memberName" />
          <el-table-column label="会员手机" align="center" prop="phoneNumber" />
          <el-table-column label="跟进教练" align="center" prop="nickName" />
        </el-table>
      </el-card>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm" v-if="isshow==1">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog :title="dialogb.title" v-model="dialogb.visible" width="580px" append-to-body>
      <el-form ref="privateTeachingScheduleFormRef" :model="formb" :rules="rules" label-width="130px">
        <el-form-item label="私教开始时间" prop="StartTime">
          <el-time-select v-model="formb.StartTime" style="width: 215px" class="mr-4" placeholder="开始时间" start="00:00"
            step="00:10" end="23:59" />
        </el-form-item>

        <el-form-item label="私教结束时间" prop="EndTime">
          <el-time-select v-model="formb.EndTime" style="width: 215px" class="mr-4" placeholder="结束时间" start="00:00"
            step="00:10" end="23:59" />
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitFormb">确 定</el-button>
          <el-button @click="cancelb">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="PrivateTeachingSchedule" lang="ts">
  import { addPrivateTeachingSchedule, updatePrivateTeachingSchedule, listPrivateTeachingSchedule, updatetime, delPrivateTeachingSchedule, getPrivateTeachingSchedule } from '@/api/primary/privateTeachingSchedule';
  import { listGym, getGym, delGym, addGym, updateGym, addguize } from '@/api/primary/gym';
  import { PrivateTeachingScheduleVO, PrivateTeachingScheduleQuery, PrivateTeachingScheduleForm } from '@/api/primary/privateTeachingSchedule/types';
  import { GymVO } from '@/api/primary/gym/types';
  import { listMember, getMember } from '@/api/primary/member';
  import { listRole } from "@/api/system/role";
  import { listCoach } from '@/api/primary/coach';
  import { LeagueClassVO } from '@/api/primary/leagueClass/types';
  import { listLeagueClass } from '@/api/primary/leagueClass';
  import { watch, nextTick } from 'vue';
  const { proxy } = getCurrentInstance() as ComponentInternalInstance;
  const { sys_yuyuestatus_type } = toRefs<any>(proxy?.useDict("sys_yuyuestatus_type"));
  const leagueClassList = ref<LeagueClassVO[]>([]);
  const privateTeachingScheduleList = ref<PrivateTeachingScheduleVO[]>([]);
  const buttonLoading = ref(false);
  const loading = ref(true);
  const showSearch = ref(true);
  const gymList = ref<GymVO[]>([]);
  const ids = ref<Array<string | number>>([]);
  const idsname = ref<Array<string | number>>([]);
  const single = ref(true);
  const multiple = ref(true);
  const total = ref(0);
  const isshow = ref(0);
  const isshowMemberCard = ref(0);
  const selectRef = ref(null);
  const isxiana = ref(0);
  const isxian = ref(0);
  const isSearch = ref(true);
  const gymFormRef = ref<ElFormInstance>();
  const memberList = ref<any>([]);
  const cardList = ref<any>([]);
  const coachcardList = ref<any>([]);
  const coachList = ref<any>([]);
  const roleCan = ref<any>();
  const queryFormRef = ref<ElFormInstance>();
  const privateTeachingScheduleFormRef = ref<ElFormInstance>();
  const roleList = ref<any>();
  const formb = reactive<any>({
    id: undefined,
    gymId: undefined,
    StartTime: undefined,
    businessHours: undefined,
    EndTime: undefined,
  });
  const dialog = reactive<DialogOption>({
    visible: false,
    title: '',
    isshow: true,//控制团课在修改中是否显示
    isfalse: false
  });
  const dialogb = reactive<DialogOption>({
    visible: false,
    title: '',
  });
  const viplist = reactive<any>({
    id: undefined,
    memberId: undefined,
    memberName: undefined,
    phoneNumber: undefined,
    classId: undefined,
    memberType: 0,
    cardId: undefined,
    memberCardVos: undefined,
  });
  const dataa = reactive({
    yuyue: [ //
      { value: 0, label: '不可以' },
      { value: 1, label: '可以' },
    ],
    state: [ //
      { value: '0', label: '已完成' },
      { value: '1', label: '已取消' },
    ],
    teachType: [ //
      { value: '0', label: '一对一' },
      { value: '1', label: '一对多' },
    ],
  })
  const initFormData : PrivateTeachingScheduleForm = {
    id: undefined,
    gymId: undefined,
    coachId: undefined,
    cardId: undefined,
    coachName: undefined,
    memberId: undefined,
    memberName: undefined,
    reservationDate: undefined,
    teachDate: undefined,
    teachType: undefined,
    isReservation: undefined,
    teachStartTime: undefined,
    StartTime: undefined,
    businessHours: undefined,
    EndTime: undefined,
    classId: undefined,//团课id
    reservationSituation: undefined,
    memberCardId: undefined,
  }
  const data = reactive<PageData<PrivateTeachingScheduleForm, PrivateTeachingScheduleQuery>>({
    form: { ...initFormData },
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      StartTime: undefined,
      EndTime: undefined,
      businessHours: undefined,
      memberName: undefined,
      memberCardId: undefined,
      gymId: undefined,
      coachId: undefined,
      cardId: undefined,
      memberId: undefined,
      startDate: undefined,//开始时间
      endDate: undefined,//结束时间
      classId: undefined,//团课id
      reservationDate: undefined,
      teachDate: undefined,
      teachStartTime: undefined,
      teachType: undefined,
      isReservation: undefined,
      reservationSituation: undefined,
      params: {
      }
    },
    rules: {
      id: [
        { required: true, message: "id不能为空", trigger: "blur" }
      ],
      gymId: [
        { required: true, message: "健身房id不能为空", trigger: "blur" }
      ],
      // coachId: [
      //   { required: true, message: "教练不能为空", trigger: "blur" }
      // ],
      memberCardId: [
        { required: true, message: "预约人会员卡不能为空", trigger: "blur" }
      ],
      cardId: [
        { required: true, message: "教练下会员卡不能为空", trigger: "blur" }
      ],
      reservationDate: [
        { required: true, message: "预约日期不能为空", trigger: "blur" }
      ],
      teachDate: [
        { required: true, message: "课程时间不能为空", trigger: "blur" }
      ],
      teachStartTime: [
        { required: true, message: "课程时间不能为空", trigger: "blur" }
      ],
      memberId: [
        { required: true, message: "", trigger: "blur" }
      ],
      // isReservation: [
      //   { required: true, message: "是否可以预约不能为空", trigger: "blur" }
      // ],
      // reservationSituation: [
      //   { required: true, message: "预约状态不能为空", trigger: "blur" }
      // ],
    }
  });

  const { queryParams, form, rules } = toRefs(data);
  // watch([() => form.value.coachId, () => form.value.memberId], ([newCoachId, newMemberId], [oldCoachId, oldMemberId]) => {
  //   if (newCoachId !== oldCoachId) {
  //     form.value.cardId = '';
  //     coachcardList.value = ''
  //   }
  //   // if (newMemberId !== oldMemberId) {
  //   //   form.value.cardId = '';
  //   // }
  // });
  watch(() => form.value.memberId, (newVal, oldVal) => {
    if (newVal == '') {
      isSearch.value = false
      form.value.memberName = ''
    }
  });
  // return { form };
  // 查询会员列表
  const getListd = async () => {
    console.log(viplist, '会员列表请求参数')
    const res = await listMember(viplist);
    memberList.value = res.rows;

  }
  const trimInput = (field) => {
    nextTick(() => {
      if (form.value[field]) {
        form.value[field] = form.value[field].trim();
      }
    });
  };
  const getListgym = async () => {
    const res = await listGym(queryParams.value);
    gymList.value = res.rows;
    let arr = gymList.value
    formb.id = arr[0].id
    console.log(formb.id, '~~~~~~~@@@@@@@@@@@@@健身房id~~~~~~~')
  }
  const handleSelectChange = (selectedId) => {
    let selectedDict = cardList.value.find(dict => dict.id === selectedId);
    if (selectedDict) {
      form.value.coachName = selectedDict.coachName
      form.value.cardId = selectedDict.cardId
      form.value.coachId = selectedDict.coachId
      console.log('选中的信息', selectedDict);
      console.log('教练姓名', form.value.coachName);
      console.log('教练id', form.value.coachId);
      console.log('CardId', form.value.cardId);
    }
  }
  /** 查询会员信息列表 */
  const getListvip = async () => {
    console.log(form.value.memberName, 'form.value.memberNameform.value.memberName')
    if (form.value.memberName == undefined) {
      proxy?.$modal.msgSuccess("请先输入会员名再进行搜索");
      return
    }
    const res = await listMember(form.value);
    memberList.value = res.rows;
    console.log(memberList.value, '正式会员表')
    if (memberList.value.length == 0) {
      isSearch.value = false
      proxy?.$modal.msgError("未搜索到该会员，请重新搜索");
    } else {

      isSearch.value = true
      openSelectDropdown()
      proxy?.$modal.msgSuccess("请选择匹配到的会员与所属的会员卡");
    }

  }
  function openSelectDropdown() {
    nextTick(() => {
      if (selectRef.value) {
        selectRef.value.focus(); // 这将打开下拉选择框
      }
    });
  }
  /** 查询私教预定日期列表 */
  const getList = async () => {
    loading.value = true;
    const res = await listPrivateTeachingSchedule(queryParams.value);
    privateTeachingScheduleList.value = res.rows;
    total.value = res.total;
    loading.value = false;
  }
  // const getListb = () => {
  //   // queryParams.value.startDate=''
  //   // queryParams.value.endDate=''
  //   listRole(proxy?.addDateRange(queryParams.value)).then(res => {
  //     roleList.value = res.rows
  //     roleCan.value = roleList.value.find(item => item.roleName === '健身教练');
  //     console.log(roleCan.value.roleId, '健身教练id')
  //     getListc()
  //   })
  // }
  /**
   * 查询教练列表
   */
  const getListb = async () => {
    console.log('111111')
    const res = await listCoach();
    coachList.value = res.rows;
    console.log(coachList.value, '教练信息表')
  }
  /** 取消按钮 */
  const cancel = () => {

    reset();
    cardList.value = []
    memberList.value = []
    isxian.value = 0
    // getListd()
    dialog.visible = false;
    dialog.isfalse = false;
    isshowMemberCard.value = 0
  }
  const cancelb = () => {
    reset();
    dialogb.visible = false;
  }
  /** 查询团课信息列表 */
  const getLista = async () => {
    // queryParams.value.startDate=''
    // queryParams.value.endDate=''
    loading.value = true;
    const res = await listLeagueClass(queryParams.value);
    leagueClassList.value = res.rows;
    console.log(leagueClassList.value, '课程信息表')
    loading.value = false;
  }

  /** 表单重置 */
  const reset = () => {
    viplist.memberId = ''
    viplist.cardId = ''
    form.value = { ...initFormData };
    privateTeachingScheduleFormRef.value?.resetFields();
  }

  /** 搜索按钮操作 */
  const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
  }

  /** 重置按钮操作 */
  const resetQuery = () => {
    queryFormRef.value?.resetFields();
    handleQuery();
  }

  /** 多选框选中数据 */
  const handleSelectionChange = (selection : PrivateTeachingScheduleVO[]) => {
    ids.value = selection.map(item => item.id);
    idsname.value = selection.map(item => item.reservationDate);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
  }

  /** 新增按钮操作 */
  const handleAdd = () => {
    reset();
    dialog.isshow = true
    dialog.visible = true;
    isshow.value = 1
    isSearch.value = false
    dialog.title = "添加私教预定日期";
  }
  /** 私教预约时间按钮操作 */
  const handledate = async () => {
    reset();
    const res = await getGym(formb.id);
    if (res.data.businessHours) {
      formb.StartTime = res.data.startTime
      formb.EndTime = res.data.endTime
    }

    dialogb.visible = true;
    dialogb.title = "添加私教预定区间日期";
  }

  /** 修改按钮操作 */
  const handleUpdate = async (row ?: PrivateTeachingScheduleVO) => {
    reset();
    isshow.value = 2
    dialog.isshow = false
    const _id = row?.id || ids.value[0]
    const res = await getPrivateTeachingSchedule(_id);
    Object.assign(form.value, res.data);
    dialog.visible = true;
    isSearch.value = false
    dialog.title = "修改私教预定日期";
  }
  const handleinfo = async (row ?: PrivateTeachingScheduleVO) => {
    reset();
    form.value.cardId = ''
    isshow.value = 3
    dialog.isshow = false
    dialog.isfalse = true
    const _id = row?.id || ids.value[0]
    const res = await getPrivateTeachingSchedule(_id);
    Object.assign(form.value, res.data);
    dialog.visible = true;
    dialog.title = "查看私教预约";
  }
  // 根据教练id获取教练下的课程名称changecoach
  // const changecoach = (e) => {
  //   coachcardList.value = ''
  //   form.value.cardId = ''
  //   console.log(e, 'eeeeeeeeeeeee')
  //   handlcoach(e)
  // }

  const changemember = (e) => {
    cardList.value = ''
    form.value.memberCardId = ''
    form.value.coachName = ''
    form.value.coachId = ''
    console.log(e, 'eeeeeeeeeeeee')
    handleuser(e)
  }
  // const handlcoach = async (id : any) => {
  //   isxian.value = 1
  //   if (id) {
  //     isxiana.value = 10
  //     const res = await getcoachcard(id);
  //     coachcardList.value = res.data
  //     proxy?.$modal.msgSuccess("请选择教练会员卡")
  //     console.log(coachcardList.value, '教练下的课')
  //   } else {
  //     proxy?.$modal.msgSuccess("请先选择会员")
  //   }
  // }
  //根据会员id获取会员下的卡
  const handleuser = async (id : any) => {

    if (id) {
      isxian.value = 11
      const res = await getMember(id);
      cardList.value = res.data.coachCardList
      isshowMemberCard.value = 4
      //proxy?.$modal.msgSuccess("请选择会员所属的卡")
      console.log(cardList.value, '会员卡')
    } else {
      proxy?.$modal.msgSuccess("请先选择会员")
    }

  }
  // 确定预约

  /** 提交按钮 */
  const submitForm = () => {
    privateTeachingScheduleFormRef.value?.validate(async (valid : boolean) => {
      if (valid) {
        buttonLoading.value = true;
        if (form.value.id) {
          await updatePrivateTeachingSchedule(form.value).finally(() => buttonLoading.value = false);
          proxy?.$modal.msgSuccess("修改成功");
        } else {
          if (form.value.cardId == '') {
            proxy?.$modal.msgSuccess("请搜索添加教练下会员卡");
            buttonLoading.value = false;
            return false
          } else {
            // form.value.memberId = form.value.memberId.join(',');
            await addPrivateTeachingSchedule(form.value).finally(() => buttonLoading.value = false);
            proxy?.$modal.msgSuccess("添加成功");
            dialog.isfalse = false;
            dialog.visible = false;
            await getList();
          }
        }

      }
    });
  }


  const submitFormb = async () => {
    formb.businessHours = formb.StartTime + ", " + formb.EndTime;
    formb.StartTime = undefined
    formb.EndTime = undefined
    buttonLoading.value = true;
    await updatetime(formb).finally(() => buttonLoading.value = false);
    proxy?.$modal.msgSuccess("修改成功");
    dialogb.visible = false;

  }
  /** 删除按钮操作 */
  const handleDelete = async (row ?: PrivateTeachingScheduleVO) => {
    const _ids = row?.id || ids.value;
    const name = row?.reservationDate || idsname.value;
    await proxy?.$modal.confirm('是否确认删除私教预约日期为"' + name + '"的数据项？').finally(() => loading.value = false);
    await delPrivateTeachingSchedule(_ids);
    proxy?.$modal.msgSuccess("删除成功");
    await getList();
  }

  /** 导出按钮操作 */
  const handleExport = () => {
    proxy?.download('primary/privateTeachingSchedule/export', {
      ...queryParams.value
    }, `privateTeachingSchedule_${new Date().getTime()}.xlsx`)
  }

  onMounted(() => {
    // 获取当前日期
    const currentDate = new Date();
    // 获取当前日期的星期几（0 表示周日，1 表示周一，以此类推）
    let currentDayOfWeek = currentDate.getDay();
    // 将星期日的索引（0）转换为 7，以便于后续计算
    if (currentDayOfWeek === 0) {
      currentDayOfWeek = 7;
    }
    // 计算最近一周的开始日期
    const startOfWeek = new Date(currentDate);
    startOfWeek.setDate(currentDate.getDate() - currentDayOfWeek + 1); // 从本周的星期一开始
    // 计算最近一周的结束日期
    const endOfWeek = new Date(startOfWeek);
    endOfWeek.setDate(startOfWeek.getDate() + 6);
    // 将日期格式转换为 ISO 8601 格式的字符串
    const startISOString = startOfWeek.toISOString();
    const endISOString = endOfWeek.toISOString();
    // 提取日期部分，忽略时间部分
    const start = startISOString.split('T')[0];
    const end = endISOString.split('T')[0];
    // 将日期格式从 yyyy-mm-dd 转换为 yyyy-mm-dd 00:00:00
    let formattedStartDate = start + ' 00:00:00';
    let formattedEndDate = end + ' 23:59:59';
    queryParams.value.startDate = formattedStartDate
    queryParams.value.endDate = formattedEndDate
    console.log("最近一周的开始日期是aa：" + queryParams.value.startDate);
    console.log("最近一周的结束日期是bb：" + queryParams.value.endDate);
    queryParams.value.pageNum = undefined
    queryParams.value.pageSize = undefined
    getLista()
    getListb()
    // getListd()
    queryParams.value.pageNum = 1
    queryParams.value.pageSize = 10
    getList();
    getListgym()
  });
</script>